//
//  WYInvitationBossController.m
//  Boss
//
//  Created by admin on 2018/7/3.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYInvitationBossController.h"
#import "WYRedPackerPersonCell.h"
#import "WYShareController.h"
#import "WYBOSSTeamController.h"
@interface WYInvitationBossController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (strong, nonatomic) IBOutlet UIImageView *userImageView;

@property (nonatomic,strong)NSString *number;
@property (nonatomic,strong)NSString *money;
@property (nonatomic,strong)NSArray *urls;

@property (strong, nonatomic) IBOutlet UILabel *moenyLabel;
@property (strong, nonatomic) IBOutlet UILabel *countLabel;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionViewWith;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *top;


@property (strong, nonatomic) IBOutlet UILabel *normalMoenyLabel;
@property (strong, nonatomic) IBOutlet UILabel *yuanLabel;
@property (strong, nonatomic) IBOutlet UIImageView *rightImageView;
@property (strong, nonatomic) IBOutlet UIButton *morebtn;

@end

@implementation WYInvitationBossController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
    self.view.backgroundColor = [UIColor hexStringToColor:@"#fffaf2" alpha:1.0];
}
-(void)loadData
{
    WEAKSELF
    [WYHttpServer getExclusivePacketSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            weakSelf.urls = response[@"result"][@"urls"];
            weakSelf.money = [response[@"result"][@"money"] stringValue];
            weakSelf.number = [response[@"result"][@"number"] stringValue];
            [weakSelf upDateUI];
        }
        
        
    }];
}
-(void)upDateUI
{
    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"已邀请%@位朋友",self.number]];
    NSRange range = [aString.string rangeOfString:self.number];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor hexStringToColor:@"#d8593f" alpha:1.0] range:range];
    [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:range];
    //self.label.attributedText = aString;
    self.countLabel.attributedText = aString;
    
    NSMutableAttributedString *bString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"今日红包剩余%.2f元",self.money.floatValue/1000.0]];
    NSRange range1 = [bString.string rangeOfString:[NSString stringWithFormat:@"%.2f",self.money.floatValue/1000.0]];
    [bString addAttribute:NSForegroundColorAttributeName value:[UIColor hexStringToColor:@"#d8593f" alpha:1.0] range:range1];
    [bString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:NSMakeRange(3, self.number.length)];
    //self.label.attributedText = aString;
    self.moenyLabel.attributedText = bString;
    
    [self.collectionView reloadData];
    
    self.collectionViewWith.constant = self.collectionView.collectionViewLayout.collectionViewContentSize.width;
    
    if (self.urls.count) {
        self.normalMoenyLabel.hidden = YES;
        self.yuanLabel.hidden = YES;
        self.collectionView.hidden = NO;
        self.rightImageView.hidden = NO;
        self.morebtn.hidden = NO;
    }else{
        
        self.normalMoenyLabel.text =[NSString stringWithFormat:@"%.2f",self.money.floatValue/1000.0];
        self.collectionView.hidden = YES;
        self.rightImageView.hidden = YES;
        self.morebtn.hidden = YES;
        self.normalMoenyLabel.hidden = NO;
        self.yuanLabel.hidden = NO;
        self.countLabel.attributedText = aString;
    }
    
    
    
    
    
}

-(void)setupUI
{
    WYUserMessage *userMessage  = [WYStoreModule currentUserMessage];
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:userMessage.url]];
    [self.userImageView setCornerRadius:33.f];
    [self.userImageView setBorderWithWidth:kLineHeight *4 andColor:[UIColor hexStringToColor:@"#fffaf2" alpha:1.0]];
    
    self.navigationItem.title = @"邀请BOSS";
    self.top.constant = self.navHeight;
    self.scrollView.delegate = self;
    [self wr_setNavBarShadowImageHidden:YES];
    
    if (@available(iOS 11.0, *)) {
        [self.scrollView setContentInsetAdjustmentBehavior:(UIScrollViewContentInsetAdjustmentNever)];
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.mainView.width = self.scrollView.width;
    self.mainView.height = 999;
    [self.scrollView addSubview:self.mainView];
    self.scrollView.contentSize = self.mainView.size;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake(36, 36);
    //横向最小距离
    layout.minimumInteritemSpacing=10.0f;
    layout.minimumLineSpacing=10.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [self.collectionView setCollectionViewLayout:layout];
    //  [layout setHeaderReferenceSize:CGSizeMake(kScreenWidth, 44)];
    [self.collectionView registerNib:[UINib nibWithNibName:@"WYRedPackerPersonCell" bundle:nil] forCellWithReuseIdentifier:@"WYRedPackerPersonCell"];
    
    
    
    
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.urls.count>6?6:self.urls.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    WYRedPackerPersonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYRedPackerPersonCell" forIndexPath:indexPath];
    if (!cell) {
        cell =[[NSBundle mainBundle] loadNibNamed:@"WYRedPackerPersonCell" owner:nil options:nil].firstObject;
    }
    [cell.personImageView setBackgroundColor:kColorMainColor];
    [cell.personImageView setCornerRadius:18.f];
//    WYReceiveUserList *list = self.dataArray[indexPath.item];
    [cell.personImageView sd_setImageWithURL:[NSURL URLWithString:self.urls[indexPath.row]] placeholderImage:[UIImage imageNamed:@"ic_defult_avatar"]];
    
    
    return cell;
}






- (IBAction)invateBoss:(id)sender {
    
    WYShareController *svc = [[WYShareController alloc]init];
    [self.navigationController pushViewController:svc animated:YES];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    if (offset.y <= 0) {
        offset.y = 0;
    }
    scrollView.contentOffset = offset;
}
- (IBAction)moreAction:(id)sender {
    WYBOSSTeamController *btv = [[WYBOSSTeamController alloc]init];
    [self.navigationController pushViewController:btv animated:YES];
}

@end
